<template>
  <div class="login-page">
    <!-- 顶部Logo (使用文本替代图片) -->
    <div class="logo-container">
      <router-link to="/">
        <img src="/logo.png" alt="LOGO" class="logo">
      </router-link>
    </div>
    
    <!-- 登录表单卡片 -->
    <div class="login-card">
      <h1 class="login-title">ログイン</h1>
      
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" @submit.prevent="handleLogin">
        <!-- 邮箱输入框 -->
        <div class="form-group">
          <label class="form-label">メールアドレス</label>
          <el-input 
            v-model="loginForm.username" 
            placeholder=" " 
            class="custom-input"
          >
          </el-input>
        </div>
        
        <!-- 密码输入框 -->
        <div class="form-group">
          <label class="form-label">パスワード</label>
          <el-input 
            v-model="loginForm.password" 
            :type="showPassword ? 'text' : 'password'" 
            placeholder=" " 
            class="custom-input"
            @keyup.enter="handleLogin"
          >
        <template #suffix>
              <el-icon 
                class="password-icon" 
                @click="showPassword = !showPassword"
              >
                <component :is="showPassword ? View : Hide" />
              </el-icon>
            </template>
          </el-input>
        </div>
        
        <!-- 登录按钮 -->
        <el-form-item class="login-btn-container">
          <el-button 
            type="primary" 
            class="login-button" 
            :loading="isLoading" 
            @click="handleLogin"
          >
            ログイン
          </el-button>
        </el-form-item>
        
        <!-- 记住登录状态 -->
        <div class="remember-me">
          <el-checkbox v-model="loginForm.remember" class="custom-checkbox">
            ログイン状態を保存する (30日間)
          </el-checkbox>
        </div>
        
        <!-- 忘记密码链接 -->
        <div class="forgot-password">
          <a href="/forget" class="link-text">パスワードをお忘れですか?</a>
        </div>
      </el-form>
    </div>
    
    <!-- 注册链接 -->
    <div class="register-section">
      <a href="/register" class="link-text">新しくアカウントを作成する</a>
    </div>
    
  <div class="footdt">
    <div>
      <span>利用規約</span>
      <span>|</span>
      <span>特定商取引法に基づく表記</span>
      <span>|</span>
      <span><a href="/article/2">個人情報保護方針</a></span>
    </div>
    <div class="copyright">
      copyright (c) WorkMatch all rights reserved.
    </div>
  </div>
  </div>
  
  <!-- 客服组件 -->
  <CustomerService />
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { View, Hide } from '@element-plus/icons-vue'
import { userApi } from '../api/index'
import { useRouter, useRoute } from 'vue-router'
import CustomerService from '../components/CustomerService.vue'

// 路由相关
const router = useRouter()
const route = useRoute()

// 表单状态
const loginFormRef = ref(null)
const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

// 显示/隐藏密码
const showPassword = ref(false)

// 登录状态
const isLoading = ref(false)

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: 'メールアドレスを入力してください', trigger: 'blur' }
  ],
  password: [
    { required: true, message: 'パスワードを入力してください', trigger: 'blur' },
    { min: 6, message: 'パスワードは6文字以上必要です', trigger: 'blur' },
    { max: 20, message: 'パスワードは20文字以下必要です', trigger: 'blur' }
  ]
}

// 重定向URL
const redirectUrl = computed(() => route.query.redirect || '/')

// 登录处理
const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      isLoading.value = true
      userApi.login(loginForm.username, loginForm.password)
        .then(res => {
          console.log(res)
          if (res.code === 1) {
            ElMessage.success( 'ログイン成功')
            localStorage.setItem('token', res.data.userinfo.token)
            localStorage.setItem('userInfo', JSON.stringify(res.data.userinfo))
            console.log(res)
            router.push(redirectUrl.value)
          } else {
            ElMessage.error('ログイン失敗：ユーザー検索エラー')
          }
        })
        .catch(error => {
          ElMessage.error('ログイン失敗：ユーザー検索エラー')
          console.error('ログインエラー:', error)
        })
        .finally(() => {
          isLoading.value = false
        })
    }
  })
}
</script>

<style scoped>
.footdt {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #666;
  font-size: 14px;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 15px 0;
  background-color: #e7f1fa;
}

.footdt .copyright {
  margin-top: 5px;
  font-size: 12px;
  color: #999;
}

.footdt span {
  margin: 0 5px;
}
.login-page {
  width: 100%;
  min-height: 95vh;
background: #e7f1fa;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
  box-sizing: border-box;
}

.logo-wrapper {
  margin-bottom: 30px;
}

.logo-text {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  letter-spacing: 0.5px;
}

.login-card {
  width: 100%;
  max-width: 350px;
  background-color: white;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  margin-bottom: 25px;
}

/* 其余样式保持不变 */
.login-title {
  text-align: center;
  font-size: 24px;
  color: #333;
  margin-bottom: 30px;
  font-weight: 500;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
      font-size: 12px;
    line-height: 1.66667;
    color: #333;
}

.custom-input {
  height: 42px;
  border-radius: 4px;
  border: 1px solid #ddd;

  width: 100%;
  font-size: 14px;

}

.custom-input::placeholder {
  color: transparent;
}

.password-toggle {
  cursor: pointer;
  padding: 0 10px;
  display: flex;
  align-items: center;
  color: #666;
}

.password-icon {
  font-size: 18px;
}

.login-btn-container {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
}

.login-button {
  width: 100%;
  height: 46px;
  background-color: #0056d2;
  border-color: #0056d2;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  color: white;
}

.remember-me {
  margin-bottom: 20px;
  text-align: center;
}

.custom-checkbox .el-checkbox__label {
  font-size: 13px;
  color: #666;
}

.forgot-password {
  text-align: center;
  margin-bottom: 10px;
}

.register-section {
  position: relative;
  bottom: -40px;
  left: 0;
  right: 0;
  text-align: center;
}

.link-text {
  color: #0056d2;
  text-decoration: none;
  font-size: 13px;
}

.link-text:hover {
  text-decoration: underline;
}



.separator {
  color: #999;
}

.copyright {
  font-size: 11px;
  color: #999;
}

/* 移除element-plus默认样式 */
:deep(.el-input__wrapper) {
  box-shadow: none !important;
  border-radius: 4px !important;
}

:deep(.el-button--primary) {
  background-color: #0056d2 !important;
  border-color: #0056d2 !important;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #0056d2 !important;
  border-color: #0056d2 !important;
}
</style>